package cn.lzx.hrm.controller;

import cn.lzx.exctption.ValidUtil;
import cn.lzx.hrm.client.ThirdPartyServiceClient;
import cn.lzx.hrm.domain.VipBase;
import cn.lzx.hrm.mapper.VipBaseMapper;
import cn.lzx.hrm.util.UserConstants;
import cn.lzx.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class VerifyCodeController {

    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Autowired
    private ThirdPartyServiceClient thirdPartyServiceClient;

    /**
     * 发送验证码的请求 远程调用三方服务处理业务
     * @param params
     * @return
     */
    @PostMapping("/verifycode/registerSmsCode")
    public AjaxResult sendRegisterSmsCode(@RequestBody Map<String,String>params){
        //空值校验
        ValidUtil.assertNotNull(params,"系统错误");
        //获取params中的参数
        String mobile = params.get("mobile");
        String imageCode = params.get("imageCode");
        String imageCodeKey = params.get("imageCodeKey");
        //空值校验
        ValidUtil.assertNotNull(mobile,"请输入正常手机号！");
        ValidUtil.assertNotNull(imageCodeKey,"请输入图片验证码");
        ValidUtil.assertNotNull(imageCode,"请输入图片验证码！");
        //远程调用三方服务的接口
        AjaxResult ajaxResult = thirdPartyServiceClient.verifyImgCode(params);
        //断言为true
        ValidUtil.assertTrue(ajaxResult.isSuccess(),ajaxResult.getMessage());
        //通过手机号查询数据库 此号码是否被注册
        List<VipBase> baseList = vipBaseMapper.selectList(new EntityWrapper<VipBase>().eq("phone", mobile));
        ValidUtil.assertListIsNull(baseList,"此号码已被注册");
        //使用map保存rides中保存的数据
        Map<String, String> paramsTmp = new HashMap<>();
        paramsTmp.put("key",UserConstants.REGISTER_CODE_PREFIX + mobile);
        paramsTmp.put("codeNum","4");
        paramsTmp.put("expire","3");
        paramsTmp.put("phone",mobile);
        return thirdPartyServiceClient.sendSmsVerifyCode(paramsTmp);
    }

}
